<?php
class CategoriesController extends AppController {
	var $name = 'Categories';
	var $uses = array('Category', 'Permission', 'PermissionGroup');
	var $layout = 'admin';
	var $helpers = array('Html', 'Javascript', 'Form', 'Time', 'Tinymce');
	var $components = array('Auth');
	var $paginate = array(
						'fields' => array('Category.id', 'Category.title'),
						'limit' => 5,
	 					'order' => array('Category.id' => 'asc')
						);

	function index() {
		$condition = array();
		if (!empty($this->passedArgs))
		{
			if (isset($this->passedArgs['Search.name']))
			{
				$condition[]["Category.name LIKE"] = "%{$this->passedArgs['Search.name']}%";
				$this->data['Search']['name'] = $this->passedArgs['Search.name'];
			}
			if(isset($this->passedArgs['Search.level']))
			{
				$condition[]["Category.level LIKE"] = $this->passedArgs['Search.level'];
				$this->data['Search']['level']	= $this->passedArgs['Search.level'];
			}
			if(isset($this->passedArgs['Search.type']))
			{
				$condition[]["Category.type LIKE"] = $this->passedArgs['Search.type'];
				$this->data['Search']['type'] = $this->passedArgs['Search.type'];
			}
		}
		
		$this->Categories->recursive = 0;
		$this->paginate = array('limit' => 6, 'page' => 1, 'order' => array('Category.created' => 'desc'));
		$categories = $this->paginate('Category', $condition);
		$this->set('categories', $categories);
		
		// Lay mang cac level trong CSDL theo:
		$lvmax = $this->Category->Query("Select MAX(level) AS lvMAX from categories");
		$arrlevel = array();
		
		for($i=0;$i<($lvmax[0][0]['lvMAX']+1);$i++)
		{
			$arrlevel[$i] = $i;
		}
		
		$this->set('levels',$arrlevel);
	}
	/*
	 * add category
	 */
	function add() 
	{
		if (!empty($this->data))
		{
			if(empty($this->data['Category']['parent_id']))
			{
				$this->data['Category']['parent_id'] = '0';
			}
			$parent = $this->Category->findById($this->data['Category']['parent_id']);	
			
			//Get Level
			
			$this->data['Category']['level'] = intval($parent['Category']['level']) + 1;
			

			$this->data['Category']['created'] = date('d/m/y');
			if ($this->Category->save($this->data))
			{
				//Update root_path:
				$menu_id = $this->Category->getLastInsertId();
				$this->Category->id = $menu_id;
				$this->Category->saveField('root_path', $parent['Category']['root_path'].$menu_id.':');
				$this->Session->setFlash('Category da duoc them');
				$this->redirect("/categories/index");
			} else {
				$this->Session->setFlash('Lưu thông tin thất bại');
			}
		}
		$parentCat = $this->Category->find('list');
		$this->set('parentCat',$parentCat);

	}
	function edit($id = null)
	{
		//Check id
		if (!$id && empty($this->data)){
			$this->Session->setFlash('Không đúng ID');
		}
		
		//Get data menu
		$category = $this->Category->findById($id);
		
		//Check data submit
		if (!empty($this->data))
		{	
			if(empty($this->data['Category']['parent_id']))
			{
				$this->data['Category']['parent_id'] = '0';
			}
			//Edit Level and Rood_path
			$parent = $this->Category->findById($this->data['Category']['parent_id']);
			$this->data['Category']['level'] = intval($parent['Category']['level']) + 1;
			$this->data['Category']['root_path'] = 	$parent['Category']['root_path'].$id.':';
			
			
			//Get menu diffirent
			$diff_level = intval($parent['Category']['level']) + 1 - intval($category['Category']['level']);
			$this->Category->id = $id;
			if ($this->Category->save($this->data))
			{
				//Update all child menu with new root_path
				$this->Category->updateAll(
								array('Category.root_path' => "replace(Category.root_path, '{$category['Category']['root_path']}', '{$this->data['Category']['root_path']}')" , 'Category.level' => 'Category.level + '. $diff_level ),
								array('Category.id <>' => $id, 'Category.root_path LIKE ' => $category['Category']['root_path'].'%')
				);
				$this->Session->setFlash('Category đã được sửa');
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash('Lưu thông tin thất bại');
			}
		}
		
		//Get data by id
		if (empty($this->data)){
			$this->data = $category;
		}
		
		//List parent menu
		$parentCat = $this->Category->find('list', array('fields' => array('id', 'name'), 'conditions' => array('Category.id <>' => $id)));
		$this->set('parentCat', $parentCat);
	}
	
	function delete($id = null)
	{
		if (!$id)
		{
			$this->Session->setFlash('Không đúng ID');
			$this->redirect ('index');
		}
		$cat = $this->Category->findById($id);
		$root_path = $cat['Category']['root_path'];
		$sql = "delete from categories where root_path LIKE '".$root_path."%'";
		if ($this->Category->query($sql))
		{
			$this->Session->setFlash('Đã xóa xong');
		}
		$this->redirect('index');
	}
}
?>